﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Threading.Tasks;
using Abp.Application.Services.Dto;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using JPW.OnlinePaperSubmissionSystem.Project.Dto;
using Kendo.DynamicLinq;
using System.Data.Entity;
using Abp.Linq.Extensions;
using Abp.Extensions;

namespace JPW.OnlinePaperSubmissionSystem.Project
{

    // [AbpAuthorize(AppPermissions.Pages_Tenant_ManuscriptStatus)]
    public class ManuscriptStatusAppService : AbpZeroTemplateAppServiceBase, IManuscriptStatusAppService
    {
        private readonly IRepository<ManuscriptStatus, long> _manuscriptStatusRepository;
        public ManuscriptStatusAppService(IRepository<ManuscriptStatus, long> manuscriptStatusRepository)
        {
            _manuscriptStatusRepository = manuscriptStatusRepository;

        }

        public DataSourceResult KendoGetManuscriptStatuss(DataSourceRequest input)
        {
            var manuscriptStatuss = _manuscriptStatusRepository
               .GetAll()
               .OrderBy(p => p.Id)
               .MapTo<List<ManuscriptStatusListDto>>().AsQueryable()
               .ToDataSourceResult(input);

            return manuscriptStatuss;
        }
        public ListResultOutput<ManuscriptStatusListDto> GetManuscriptStatuss(GetManuscriptStatusInput input)
        {
            var manuscriptStatuss = _manuscriptStatusRepository
            .GetAll().WhereIf(
                    !input.Filter.IsNullOrWhiteSpace(),
                    u =>
                        u.ManuscriptStatusName.Contains(input.Filter) ||
                        u.Code.Contains(input.Filter) ||
                        u.Order.Equals(Convert.ToInt32(input.Filter))
                ).OrderBy(u => u.Id)
            .ToList();
            return new ListResultOutput<ManuscriptStatusListDto>(manuscriptStatuss.MapTo<List<ManuscriptStatusListDto>>());
        }
        public async Task<ListResultOutput<ManuscriptStatusListDto>> GetManuscriptStatussAsync()
        {
            var query = await _manuscriptStatusRepository.GetAll().OrderBy(p => p.Id).ToListAsync();

            return new ListResultOutput<ManuscriptStatusListDto>(query.MapTo<List<ManuscriptStatusListDto>>());

        }

        //  [AbpAuthorize(AppPermissions.Pages_Tenant_ManuscriptStatus_Delete)]
        public async Task DeleteManuscriptStatus(IdInput input)
        {
            await _manuscriptStatusRepository.DeleteAsync(input.Id);
        }

        public async Task CreateOrUpdateManuscriptStatus(CreateOrUpdateManuscriptStatusInput input)
        {
            if (input.ManuscriptStatus.ID.HasValue)
            {
                await UpdateManuscriptStatusAsync(input);
            }
            else
            {
                await CreateManuscriptStatusAsync(input);
            }
        }
        //  [AbpAuthorize(AppPermissions.Pages_Tenant_ManuscriptStatus_Create)]
        public async Task CreateManuscriptStatusAsync(CreateOrUpdateManuscriptStatusInput input)
        {
            var manuscriptStatus = input.ManuscriptStatus.MapTo<ManuscriptStatus>();
            await _manuscriptStatusRepository.InsertAsync(manuscriptStatus);
        }
        //  [AbpAuthorize(AppPermissions.Pages_Tenant_ManuscriptStatus_Update)]
        public async Task UpdateManuscriptStatusAsync(CreateOrUpdateManuscriptStatusInput input)
        {
            var manuscriptStatus = input.ManuscriptStatus.MapTo<ManuscriptStatus>();
            await _manuscriptStatusRepository.UpdateAsync(manuscriptStatus);
        }
        //  [AbpAuthorize(AppPermissions.Pages_Tenant_ManuscriptStatus_Update)]
        public async Task<GetManuscriptStatusForEditOutput> GetManuscriptStatusForEdit(NullableIdInput input)
        {
            ManuscriptStatus manuscriptStatus = null;
            if (input.Id.HasValue) //Editing existing role?
            {
                manuscriptStatus = await _manuscriptStatusRepository.FirstOrDefaultAsync(input.Id.Value);
            }
            return new GetManuscriptStatusForEditOutput
            {
                ManuscriptStatus = manuscriptStatus.MapTo<ManuscriptStatusEditDto>(),
            };
        }
    }
}

